#ifdef VECTOR_H
#define VECTOR_H


#include<algorithm>
#include<iostream>

template<typename Object>


class Vector
{
private:
    int theSize;
    int theCapacity;
    Object *object;
    /* data */
public:
    static const int SPARE_CAPACITY = 16;
    explicit Vector(int initSize = 0): theSize{initSize},
    theCapacity{initSize + SPARE_CAPACITY}
    {
        object = new Object[theCapacity];
    }//初试化构造函数

    Vector(const Vector &rhs) : theSize{rhs.theSize},  //拷贝构造函数
    theCapacity{rhs.theCapacity},object{nullptr}{
        object = new Object[theCapacity];
        for(int k = 0;k<theSize;k++){
            object[k] = rhs.object[k];
        }
    }

    Vector & operator=(const Vector& rhs){   //深拷贝
        Vector copy = rhs;
        std::swap(*this,copy);
        return *this;
    }

    ~Vector(){
        delete [] object;//析构
    }
    Vector(Vector && rhs) : theSize{rhs.theSize},//右值引用
    theCapacity{rhs.theCapacity},object{rhs.object}{
        rhs.object = nullptr;
        rhs.theCapacity = 0;
        rhs.theSize = 0;

    }
    Vector & operator=(Vector && rhs){  //移动赋值
        std::swap(theSize,rhs.theSize);
        std::swap(theCapacity,rhs.theCapacity);
        std::swap(object,rhs.object);


        return *this;
    }

    void resize(int newSize){
        if(newSize > theCapacity)
            reserve(newSize*2);
        theSize = newSize;
    }

    void reserve(int newCapacity){
        if(newCapacity<theSize)
            return;
        Object *newArray = new Object[newCapacity];
        for(int k =0; k < theSize;k++){
            newArray[k] = object[k];

        }
        theCapacity = newCapacity;
        std::swap(object,newArray);
        delete [] newArray;
    }
    Object & operator[](int index){
        return object[index];
    }

    const Object & operator[](int index) const{    //修饰返回值的const,防止返回值成为左值
        return object[index];
    }

    bool empty() const{
        return size() ==0;
    }

    int size() const{   
        return theSize;
    }
    int Capacoty() const{
        return theCapacity;
    }
    void push_back(const Object &x){
        if(theSize == theCapacity){
            reserve(2*theCapacity+1);
        }
        object[theSize++] = x;
    }
    void push_back(Object &&x){
        if(theSize == theCapacity){
            reserve(2*theCapacity+1);
        }
        object[theSize++] = std::move(x);
    }
    void pop_back(){
        if(!empty())
            --theSize;
        
    }
    const Object & back () const{
        return object[theSize-1];
    }
    typedef Object * iterator;
    typedef const Object * const_iterator;

    iterator begin(){
        return &object[0];
    }
    const_iterator begin() const{
        return &object[0];
    }

    iterator end(){
        return &object[size()];
    }

    const_iterator end() const{
        return &object[size()];
    }








    
};


#endif